package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierDao;
import cn.itcast.bos.dao.base.FixedAreaDao;
import cn.itcast.bos.dao.base.TakeTimeDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;

@Service//交给spring管理
@Transactional//事务管理
public class FixedAreaServiceImpl implements FixedAreaService {

	//注入dao层
   @Autowired
   private FixedAreaDao fixedAreaDao;
   //定区关联快递员需要快递员和派件时间的持久层
   @Autowired
   private  CourierDao courierDao;
	
   @Autowired
   private  TakeTimeDao takeTimeDao;
	
	@Override
	public void save(FixedArea fixedArea) {
	
		fixedAreaDao.save(fixedArea);
	}


	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification, Pageable pageable) {
		
		return fixedAreaDao.findAll(specification, pageable);
	}


	@Override
	public void associationCourierToFixedArea(Integer courierId, Integer takeTimeId, String id) {
		//首先根据定区id在数据中查询到对应的定区信息
		FixedArea fixedArea = fixedAreaDao.findOne(id);
		
		//根据快递员id信息查询
		Courier courier = courierDao.findOne(courierId);
		
		//根据派件时间id查询派件信息
		TakeTime takeTime = takeTimeDao.findOne(takeTimeId);
		
		//数据表快递员和定区的外键维护是交给定区维护的
		fixedArea.getCouriers().add(courier);
		//快递员与 taketime是多对一的关系，外键维护是交给多的一方管理
		
		courier.setTakeTime(takeTime);
	}

}
